﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sicurezza.Client.Controlli.CA;
using System.Security;

namespace Sicurezza.Client.Manager
{
    public class WSManager
    {
        private static CAService client = new CAService();

        public static string SendRequest(string request)
        {
            SignedMessage sm = client.SendRequest(request);
            if (SSLManager.CheckSign(sm))
                return sm.Message;
            else
                return string.Empty;
        }

        public static string GetCertificate(string ReqId)
        {
            SignedMessage sm = client.GetCertificate(ReqId);
            
            if (sm!=null && SSLManager.CheckSign(sm))
                return sm.Message;
            else
                return string.Empty;
        }


        public static bool Revoke(string serial, byte[] signature)
        {
            
            SignedMessage sm = client.RequestRevoke(serial, signature);

            if (sm != null && SSLManager.CheckSign(sm))
                return Convert.ToBoolean(sm.Message);

            return false;
        }


        public static string Renew(string serial, string newkeyPEM, byte[] signature)
        {

            SignedMessage sm = client.RequestRenew(serial,newkeyPEM, signature);
            if (SSLManager.CheckSign(sm))
                return sm.Message;
            else
                return string.Empty;
        }

        public static string ValidateByOcsp(int serial)
        {
            SignedMessage sm = client.OCSPRequest(serial);

            if (sm != null && SSLManager.CheckSign(sm))
                return sm.Message;

            return string.Empty;
        }

        public static string GetRecipients(string serial, string signature, string encipher, byte[] sign)
        {
            SignedMessage sm = client.GetRecipients(serial,signature, encipher, sign);
            sm.Message = sm.Message.Replace("\n", "\r\n");
            if (SSLManager.CheckSign(sm))
                return sm.Message;
            else
                return string.Empty;

        }
    }
}
